package com.teach.food.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.teach.dto.FoodGoodsDTO;
import com.teach.dto.FoodOrderDTO;
import com.teach.dto.HotelOrderDTO;
import com.teach.dto.LoginUser;
import com.teach.entity.Shops;
import com.teach.food.service.FoodGoodsService;
import com.teach.food.service.FoodMerchantService;
import com.teach.food.service.FoodOrderInfoService;
import com.teach.service.ShopsService;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

@Controller
@Slf4j
public class FoodIndexController {

	@Autowired
	private FoodMerchantService service;
	
	@Autowired
	private MongoTemplate template;
	
	@Autowired
	private ShopsService service1;
	
	@Autowired
	private FoodGoodsDTO good;
	
	@Autowired
	private FoodGoodsService goodSer;
	
	@Autowired
	private FoodOrderInfoService orSer;
	
	@GetMapping("/food.html")
	public String doFood(Model model) {
		Query query = new Query();
		query.addCriteria(Criteria.where("shopTypeId").is(0));
		query.addCriteria(Criteria.where("shopStatus").is(2));
		List<Shops> list = template.find(query, Shops.class);
//		Collections.sort(list,new Comparator<Shops>() {
//
//			@Override
//			public int compare(Shops o1, Shops o2) {
//				
//				return o1.getId().compareTo(o2.getId());
//			}
//		});
		log.debug("666666666{}",list);
//		Collections.shuffle(list);
		model.addAttribute("list", list);
		List<Shops> list1 = list.subList(3, 8);
		List<Shops> list2 = list.subList(0, 3);
		List<Shops> list3 = list.subList(0, 1);
		List<Shops> lis= list.subList(3, 5);
		
//		log.debug("55555555{}",list2);
		model.addAttribute("list1", list1);
		model.addAttribute("list2", list2);
		model.addAttribute("list3", list3);
		model.addAttribute("list", lis);
		return "food/index";
//		Map<String, Integer> map = new HashMap();
//		for (Shops shops : list) {
//			if (shops.getShopTypeId()==10) {
//				
//				map.put(shops.getShopTypeId().toString(), map.get(shops.getShopTypeId().toString()));
//				log.debug("111111{}",map);
//			} else {
//
//			}
//			shops.getShopTypeId();
//		}
		 
	}
	
	/**
	 * 商家列表
	 * @param model
	 * @return
	 */
	@RequestMapping("merchant.do")
	public Object foodList(Model model) {
		return model.addAttribute("list",service.selectAll());
	}
	
	@GetMapping("shangj.do")
	public String doFoodInfo(Long id, Shops shop, Model model) {
		shop = service1.get(id);
		log.debug("1111111111{}",shop);
		String map = "gis-show.html#"+String.valueOf(shop.getPoint().getX())+"|"+String.valueOf(shop.getPoint().getY())+"|"+shop.getShopName();
		log.debug("33333{}",map);
		model.addAttribute("map", map);
		model.addAttribute("map1", shop);
//		List<FoodGoodsDTO> map2 = goodSer.getFoodGoodsDTO(id);
//		log.debug("22222222{}",map2);
//		model.addAttribute("map2", map2);
		return "food/shangjia";
	}
	
	@GetMapping("/food-search.do")
	public String doList(Shops shop,Model model,HttpServletRequest request) {
		Query query = new Query();
		String shopName = request.getParameter("input_search");
		log.debug("343434343{}",shopName);
		Pattern pattern = Pattern.compile("^.*"+shopName+".*$", Pattern.CASE_INSENSITIVE);
		query.addCriteria(Criteria.where("shopTypeId").is(0));
		query.addCriteria(Criteria.where("shopName").regex(pattern));
		query.addCriteria(Criteria.where("shopStatus").is(2));
		List<Shops> shops = template.find(query, Shops.class);
		log.debug("11111111111111111{}",shops);
		model.addAttribute("foodSearch",shops);
		return "food/food-list";
	}
	
	
	/**
	 * 退出当前登录
	 * @param req
	 * @return
	 */
	@RequestMapping("logout.do")
	public String logout(HttpServletRequest req) {
		req.getSession().invalidate();
		return "forward:food/index";
		
	}
	
//	@RequestMapping("food-order.do")
//	@ResponseBody
//	public List<FoodOrderDTO> doOrderList(Shops shop,LoginUser user) {
//		System.out.println(user.getUserName());
//		
//		List<FoodOrderDTO> orderDTOs = orSer.listByuserName(user.getUserName());
//		log.debug("2222222{}",orderDTOs);
//		List<FoodOrderDTO> dtos = new ArrayList<>();
//		for (int i = 0; i < orderDTOs.size(); i++) {
//			shop = service1.get(orderDTOs.get(i).getShopId());
//			String shopName = shop.getShopName();
//			FoodOrderDTO dto = new FoodOrderDTO();
//			dto.setOrderId(orderDTOs.get(i).getOrderId());
//			dto.setFoodOrderName(orderDTOs.get(i).getFoodOrderName());
//			dto.setFoodOrderNumber(orderDTOs.get(i).getFoodOrderNumber());
//			dto.setFoodOrderPrice(orderDTOs.get(i).getFoodOrderPrice());
//			dto.setOrderStatus(orderDTOs.get(i).getOrderStatus());
//			dto.setShopName(shopName);
//			dto.setUserName(orderDTOs.get(i).getUserName());
//			dtos.add(dto);
//		}
//		return dtos;
//	}
	
	@RequestMapping("food-order.do")
	@ResponseBody
	public List<FoodOrderDTO> myOrderDo(Shops shop, LoginUser user) {

		List<FoodOrderDTO> orderDTOs = orSer.listByuserName(user.getUserName());
		List<FoodOrderDTO> dtos = new ArrayList<>();
		for (int i = 0; i < orderDTOs.size(); i++) {
			shop = service1.get(orderDTOs.get(i).getShopId());
//			String shopName = shop.getShopName();
			FoodOrderDTO dto = new FoodOrderDTO();
			dto.setFoodOrderInfoId(orderDTOs.get(i).getFoodOrderInfoId());
			dto.setFoodOrderInfoContent(orderDTOs.get(i).getFoodOrderInfoContent());
			dto.setFoodOrderNumber(orderDTOs.get(i).getFoodOrderNumber());
			dto.setFoodOrderInfoPrice(orderDTOs.get(i).getFoodOrderInfoPrice());
			dto.setFoodOrderInfoState(orderDTOs.get(i).getFoodOrderInfoState());
			if (orderDTOs.get(i).getFoodOrderInfoState().equals("1")) {
				dto.setFoodOrderInfoState("已收货");
			} else {
				dto.setFoodOrderInfoState("未收货");
			}
			dto.setFoodOrderMerchantName(orderDTOs.get(i).getFoodOrderMerchantName());
			dto.setFoodOrderInfoUserName(orderDTOs.get(i).getFoodOrderInfoUserName());
			dtos.add(dto);
		}
		return dtos;
	}
	
	@GetMapping("food/person-Info.html")
	public String personInfo() {
		return "user.html";
		
	}
}
